#ifndef TILEMAPGENER_H
#define TILEMAPGENER_H

#include <vector>
#include <ctime>
#include <mapbase.h>

class TileMapGenerator
{
private:
    int width;
    int height;
    int seed;
    std::vector<std::vector<int>> map;
public:
    TileMapGenerator(int width, int height, int seed) : width(width), height(height), seed(seed){
        srand(time(NULL));
    }

    TileMapGenerator& generate_base()
    {
        map.clear();
        // 随机填充
        for(int i=0; i < height; i++)
        {
            std::vector<int> row;
            for(int j=0; j < width; j++)
            {
                row.push_back(rand()%4);
            }
            map.push_back(row);
        }
        // 转置
        std::vector<std::vector<int>> result(width, std::vector<int>(height));
    
        for (size_t i = 0; i < height; i++)
            for (size_t j = 0; j < width; j++)
                result[j][i] = map[i][j];
        map.clear();
        map = std::move(result);
        // 边缘化
        for(int i=0; i < height; i++){
            map[0][i] = Blocks::value::wall;
            map[width-1][i] = Blocks::value::wall;
        }
        for(int j=0; j < width; j++){
            map[j][0] = Blocks::value::wall;
            map[j][height-1] = Blocks::value::wall;
        }
        // 放置终起点
        map[rand()%(width-2)+1][rand()%(height-2)+1] = Blocks::value::start;
        map[rand()%(width-2)+1][rand()%(height-2)+1] = Blocks::value::end;
        return *this;
    }

    std::vector<std::vector<int>> getMap() const {
        return map;
    }

    int getWidth() const {
        return width;
    }
    int getHeight() const {
        return height;
    }
};


#endif